package com.oriental.manage.controller.specially;

import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.utils.StringUtils;
import com.oriental.manage.core.utils.XstreamUtil;
import com.oriental.manage.dao.specially.EnterpriseMchntBaseInfoMapper;
import com.oriental.manage.model.specially.EnterpriseMchntBaseInfo;
import com.oriental.manage.pojo.specially.TSendMessageDto;
import com.oriental.manage.service.specially.SendMessageService;
//import com.oriental.riskfront.api.model.specially.BlackListPushBody;
//import com.oriental.riskfront.api.model.specially.BlackListPushInfoBody;
import com.oriental.riskfront.api.model.specially.BlackListPushBody;
import com.oriental.riskfront.api.model.specially.BlackListPushInfoBody;
import com.oriental.riskfront.api.model.specially.ResponseModel;
import com.oriental.riskfront.api.model.specially.TBlackListDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sTrAnGeR on 2017/4/25.
 */

@Slf4j
@Controller
@RequestMapping("riskInfo/sendMessageQuery")
public class SendMessageController {

    @Autowired
    private SendMessageService sendMessageService;

    @Autowired
    EnterpriseMchntBaseInfoMapper enterpriseMchntBaseInfoMapper;

    @RequestMapping("/init")
    public String init(){
        return "specially/searchSendMessage";
    }

    @RequestMapping("/toDetail")
    public String toDetail(){
        return "specially/searchSendMessageDetail";
    }

    @RequestMapping("/search")
    @ResponseBody
    public ResponseModel<Pagination<com.oriental.riskfront.api.model.specially.TSendMessageDto>> search(Pagination<com.oriental.riskfront.api.model.specially.TSendMessageDto> pagination, TSendMessageDto tSendMessageDto){
        ResponseModel<Pagination<com.oriental.riskfront.api.model.specially.TSendMessageDto>> responseModel = new ResponseModel<Pagination<com.oriental.riskfront.api.model.specially.TSendMessageDto>>();
        try{
            sendMessageService.queryTSendMessage(pagination,tSendMessageDto,responseModel);
            log.info("查询报文记录信息:{},{}", tSendMessageDto, pagination);
            responseModel.setSuccess(true);
            responseModel.setResult(pagination);
        }catch (Exception e){
            log.error("查询报文记录信息失败", e);
        }
        return responseModel;
    }

    @RequestMapping("/checkMchntInfo.do")
    @ResponseBody
    public String checkMchntInfo(String xml){
        log.info("检查商户信息是否有效:{}",xml);
        String resultXml = null;
        try{
            BlackListPushBody blackListPushBody = XstreamUtil.xmlToObject(xml, BlackListPushBody.class);
            List<BlackListPushInfoBody> infos = blackListPushBody.getRiskInfo();

            List<EnterpriseMchntBaseInfo> responseInfo = new ArrayList<EnterpriseMchntBaseInfo>();
            for(BlackListPushInfoBody body : infos){
                EnterpriseMchntBaseInfo enterpriseMchnt = new EnterpriseMchntBaseInfo();
                enterpriseMchnt.setRegName(body.getRegName());
                enterpriseMchnt.setCusName(body.getCusName());
                enterpriseMchnt.setDocType(body.getDocType());
                enterpriseMchnt.setDocCode(body.getDocCode());
                enterpriseMchnt.setLegDocName(body.getLegDocName());
                enterpriseMchnt.setLegDocType(body.getLegDocType());
                enterpriseMchnt.setLegDocCode(body.getLegDocCode());
                List<EnterpriseMchntBaseInfo> enterpriseMchntBaseInfoList = enterpriseMchntBaseInfoMapper.pageQuery(enterpriseMchnt);
                if(enterpriseMchntBaseInfoList!= null && enterpriseMchntBaseInfoList.size() > 0){
                    for(EnterpriseMchntBaseInfo info : enterpriseMchntBaseInfoList){
                        responseInfo.add(info);
                    }
                }
            }
            String responseXml = XstreamUtil.toXml(responseInfo);
            return responseXml;
        }catch (Exception e){
            log.error("检查商户信息是否有效异常",e);
        }
        return null;
    }

}
